using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _2DGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class EffectObject : DrawObject
    {
        protected Vector2 position;
        protected Vector2 velocity;
        protected Vector2 acceleration;

        protected Color col;
        protected float size;
        protected float sizeChange;
        protected float lifespan;

        public EffectObject(
            String fromFilename,
            SpriteBlendMode fromBlendMode,
            Vector2 fromPosition,
            Vector2 fromVelocity, 
            Vector2 fromAcceleration, 
            Color fromCol,
            float fromScale,
            float fromScaleChange,
            float fromLifespan)
            : base(fromFilename, fromBlendMode)
        {
            position = fromPosition;
            velocity = fromVelocity;
            acceleration = fromAcceleration;
            col = fromCol;
            size = fromScale;
            sizeChange = fromScaleChange;
            lifespan = fromLifespan;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public virtual void Update(GameTime gameTime)
        {
            lifespan -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (lifespan <= 0.0f)
            {
                Resources.World.RemoveObject(this);
                return;
            }

            velocity += acceleration * (float)gameTime.ElapsedGameTime.TotalSeconds;
            position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

            size += sizeChange * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (size <= 0.0f)
            {
                size = 0.0f;
                Resources.World.RemoveObject(this);
            }

            // TODO: Add your update code here
        }

        public override void Draw()
        {
            Point screenPosition = Resources.World.GetScreenPosition(position);

            Rectangle screenRect = new Rectangle();

            screenRect.X = screenPosition.X - (int)(size * Resources.World.DrawScale / 2.0f);
            screenRect.Y = screenPosition.Y - (int)(size * Resources.World.DrawScale / 2.0f);
            screenRect.Width = (int)(size * Resources.World.DrawScale);
            screenRect.Height = (int)(size * Resources.World.DrawScale);

            if (screenRect.Width <= 0)
                screenRect.Width = 1;
            if (screenRect.Height <= 0)
                screenRect.Height = 1;

            Resources.SpriteBatch.Draw(
                Texture,
                screenRect,
                col
                );
       }
    }
}